package com.ruoyi.web.controller.sxt;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SxtBumen;
import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.sxt.domain.SxtGangwei;
import com.ruoyi.sxt.domain.SxtYonghu;
import com.ruoyi.sxt.service.ISxtBumenService;
import com.ruoyi.sxt.service.ISxtGangweiService;
import com.ruoyi.sxt.service.ISxtJueseService;
import com.ruoyi.sxt.service.ISxtYonghuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/sxt/yonghu")
public class SxtYonghuController extends BaseController
{
    @Autowired
    private ISxtYonghuService sxtYonghuService;

    @Autowired
    private ISxtBumenService bumenService;

    @Autowired
    private ISxtGangweiService gangweiService;

    @Autowired
    private ISxtJueseService jueseService;


    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:list')")
    @GetMapping("/list")
    public TableDataInfo list(SxtYonghu sxtYonghu)
    {
        startPage();
        List<SxtYonghu> list = sxtYonghuService.selectSxtYonghuList(sxtYonghu);
        if(list != null && list.size() > 0){
            for(SxtYonghu yonghu : list){
                SxtBumen bumen = bumenService.selectSxtBumenByDeptId(yonghu.getDeptId());
                yonghu.setDeptName(bumen.getDeptName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SxtYonghu sxtYonghu)
    {
        List<SxtYonghu> list = sxtYonghuService.selectSxtYonghuList(sxtYonghu);
        ExcelUtil<SxtYonghu> util = new ExcelUtil<SxtYonghu>(SxtYonghu.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(sxtYonghuService.selectSxtYonghuByUserId(userId));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SxtYonghu sxtYonghu)
    {
        return toAjax(sxtYonghuService.insertSxtYonghu(sxtYonghu));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SxtYonghu sxtYonghu)
    {
        return toAjax(sxtYonghuService.updateSxtYonghu(sxtYonghu));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(sxtYonghuService.deleteSxtYonghuByUserIds(userIds));
    }

    /**
     * 修改密码
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SxtYonghu sxtYonghu)
    {
        return toAjax(sxtYonghuService.resetPwd(sxtYonghu));
    }

    /**
     * 修改用户状态
     */
    @PreAuthorize("@ss.hasPermi('sxt:yonghu:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/change_yonghu_status")
    public AjaxResult changeYonghuStatus(@RequestBody SxtYonghu sxtYonghu)
    {
        return toAjax(sxtYonghuService.updateSxtYonghu(sxtYonghu));
    }

    /**
     * 根据用户编号获取详细添加信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/get_add_info")
    public AjaxResult getAddInfo(SxtGangwei gangwei, SxtJuese juese)
    {
        AjaxResult ajax = AjaxResult.success();
        List<SxtJuese> jueseList = jueseService.selectSxtJueseList(juese);
        if(jueseList != null && jueseList.size() > 0){
            for(SxtJuese sxtJuese : jueseList){
                if(sxtJuese.getRoleId() == 1){
                    jueseList.remove(sxtJuese);
                    break;
                }
            }
        }
        ajax.put("gangweis", gangweiService.selectSxtGangweiList(gangwei));
        ajax.put("jueses", jueseList);
        return ajax;
    }

    /**
     * 根据用户编号获取详细修改信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/get_update_info/{userId}")
    public AjaxResult getUpdateInfo(SxtGangwei gangwei, SxtJuese juese, @PathVariable("userId") Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        List<SxtJuese> jueseList = jueseService.selectSxtJueseList(juese);
        if(jueseList != null && jueseList.size() > 0){
            for(SxtJuese sxtJuese : jueseList){
                if(sxtJuese.getRoleId() == 1){
                    jueseList.remove(sxtJuese);
                    break;
                }
            }
        }
        List<SxtGangwei> gangweiList = gangweiService.selectSxtGangweiList(gangwei);
        List<Long> gangweiIds = sxtYonghuService.selectSxtYonghuListByGangweiId(userId);
        List<Long> jueseIds = sxtYonghuService.selectSxtYonghuListByJueseId(userId);
        SxtYonghu sxtYonghu = sxtYonghuService.selectSxtYonghuByUserId(userId);
        ajax.put("data", sxtYonghu);
        ajax.put("gangweiIds", gangweiIds);
        ajax.put("jueseIds", jueseIds);
        ajax.put("gangweis", gangweiList);
        ajax.put("jueses", jueseList);
        return ajax;
    }

    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/authRole/{userId}")
    public AjaxResult authRole(@PathVariable("userId") Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        SxtYonghu sxtYonghu = sxtYonghuService.selectSxtYonghuByUserId(userId);
        List<SxtJuese> jueseList = sxtYonghuService.selectSxtJueseList(userId);
        if(jueseList != null && jueseList.size() > 0){
            for(SxtJuese sxtJuese : jueseList){
                if(sxtJuese.getRoleId() == 1){
                    jueseList.remove(sxtJuese);
                    break;
                }
            }
        }
        ajax.put("user", sxtYonghu);
        ajax.put("roles", jueseList);
        return ajax;
    }

    /**
     * 用户授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PutMapping("/authRoleSave")
    public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
    {
        sxtYonghuService.insertAuthJuese(userId,roleIds);
        return success();
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/bumenTree")
    public AjaxResult bumenTree(SxtBumen bumen)
    {
        return success(bumenService.selectBumenTreeList(bumen));
    }
}
